package com.lz.football.module.techBar.view
{
	import com.common.stdlib.ArrayUtil;
	import com.common.utils.Reflection;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.lz.football.module.base.IComponent;
	
	import common.item.GoodsUseItem;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import lz.controls.PageComponent;
	import lz.controls.events.ListEvent;
	import lz.controls.events.PageEvent;

	/**
	 *显示背包里的物品 
	 * @author lake
	 * 
	 */	
	public class ShowGoodsPanel extends Sprite implements IComponent
	{
		/**
		 * 皮肤
		 */		
		public var _symbolSkin:MovieClip;
		/**
		 *物品显示容器 
		 */		
		private var container :MovieClip;
		/**
		 *分页 
		 */		
		private var pageComp 		:PageComponent ;
		/**
		 *item 列表 
		 */		
		private var itemList		:Array = [];
		/**
		 *要显示的物品列表 
		 */		
		private var _goodsList		:Array = [];
		/**
		 *选择的条目 
		 */		
		public var selectItem		:GoodsUseItem;
		/**
		 * 是否显示选中边框
		 */		
		public var isShowSelect	:Boolean = false;
		
		public function ShowGoodsPanel()
		{
			super();
		}
		/**
		 *设置获取商品列表 
		 * @param value
		 * 
		 */		
		public function set goodsList(value :Array):void
		{
			_goodsList = value;
			if(_goodsList == null)
			{
				return;
			}
			_goodsList.sortOn(["ntype","color"],[Array.NUMERIC,Array.NUMERIC]);
			//分页设置
			pageComp.totalNumbers = _goodsList.length;
			pageComp.pageSize = 16;
			pageComp.curPage = pageComp.curPage;
		}
		public function get goodsList():Array
		{
			return _goodsList;
		}
		/**
		 *获取同类物品
		 * 如一级进攻属性卡 
		 * @param goodsVo
		 * @return 
		 * 
		 */		
		public function getSameGoods(goodsVo :GoodsVo):Array
		{
			var res :Array = [];
			for each(var gvo :GoodsVo in goodsList)
			{
				if(gvo.type == goodsVo.type && gvo.gid == goodsVo.gid)
				{
					res.push(gvo);
				}
			}
			return res;
		}
		/**
		 *获取同类物品
		 * 如 球魂 碎片 
		 * @param goodsVo
		 * @return 
		 * 
		 */		
		public function getSameNtypeGoods(goodsVo :GoodsVo):Array
		{
			var res :Array = [];
			for each(var gvo :GoodsVo in goodsList)
			{
				if(gvo.ntype == goodsVo.ntype)
				{
					res.push(gvo);
				}
			}
			return res;
		}
		/**
		 *增加物品 
		 * @param goodsVo
		 * 
		 */		
		public function addGoods(goodsVo :GoodsVo):void
		{
			_goodsList.push(goodsVo);
			_goodsList.sortOn(["ntype","color"],[Array.NUMERIC,Array.NUMERIC]);
			pageComp.curPage = pageComp.curPage;
		}
		/**
		 *删除物品 
		 * @param goodsVo
		 * 
		 */		
		public function removeGoods(goodsVo :GoodsVo):void
		{
			_goodsList.splice(_goodsList.indexOf(goodsVo),1);
			pageComp.curPage = pageComp.curPage;
		}
		/**
		 *初始化 
		 * 
		 */	
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.techbar.view.ShowGoodsSkin");
			this.addChild(_symbolSkin);
			container = _symbolSkin.container;
			container.x = 4;
			container.y = 4;
			//分页初始化
			pageComp = new PageComponent();
			var pageCompSkin :MovieClip = Reflection.createInstance("com.liangzhong.ui.pageComp");
			pageComp.initUI(pageCompSkin);
			_symbolSkin.addChild(pageCompSkin);
			pageCompSkin.x = 70;
			pageCompSkin.y = 236;
			//初始化格子
			var i :int = 0;
			var uItem :GoodsUseItem;
			for(;i<16;i++)
			{
				uItem = new GoodsUseItem();
				uItem.initUI();
				uItem.x = (i % 4) * 58;
				uItem.y = int(i / 4) * 58;
				container.addChild(uItem);
				itemList.push(uItem);
			}
			//事件
			addEvtListener();
		}
		/**
		 *注册事件 
		 * 
		 */	
		public function addEvtListener():void
		{
			pageComp.addEventListener(PageEvent.PAGE_CHANGE,pageChangeHandler);
			this.addEventListener(ListEvent.ITEM_CLICK,itemClickHandler);
		}
		/**
		 *页数改变 
		 * @param e
		 * 
		 */		
		private function pageChangeHandler(e :PageEvent):void
		{
			if(selectItem)
			{
				selectItem.unSelect();
				selectItem = null;
			}
			//显示数据
			setItemPageDatas(ArrayUtil.getPageSizeDatas(pageComp.curPage,pageComp.pageSize,_goodsList));
		}
		/**
		 *设置数据 
		 * @param datas
		 * 
		 */		
		public function setItemPageDatas(datas :Array):void
		{
			var item :GoodsUseItem; 
			var i :int = 0;
			for each(var itemObj :* in datas)
			{
				if(i>=16)return;
				item = itemList[i];
				item.data = itemObj;
				container.addChild(item);
				i ++;
			}
			for(;i < 16;i++)
			{
				item = itemList[i];
				item.data = null;
			}
		}
		/**
		 *物品选中 
		 * @param e
		 * 
		 */		
		private function itemClickHandler(e :ListEvent):void
		{
			if(selectItem)
			{
				selectItem.unSelect();
				selectItem = null;
			}
			selectItem = e.target as GoodsUseItem;
			if(isShowSelect)
			{
				selectItem.select();
			}
		}
		/**
		 *删除事件 
		 * 
		 */	
		public function removeEvtListener():void
		{
			pageComp.removeEventListener(PageEvent.PAGE_CHANGE,pageChangeHandler);
			this.removeEventListener(ListEvent.ITEM_CLICK,itemClickHandler);
		}
		/**
		 *销毁 
		 * 
		 */	
		public function destroyView():void
		{
			removeEvtListener();
			pageComp = null;
			itemList = null;
			_goodsList = null;
		}
	}
}